package database;

import java.util.Scanner;
import java.util.InputMismatchException;

public class Program {
	
	public static boolean quit = false; 
	public static Scanner scanner = new Scanner(System.in);
	public static Scanner num = new Scanner(System.in);
	public static DatabaseSVN mainDatabase = new DatabaseSVN();
	public static String userCat;
	public static String userChoice = "";
	public static String userChoice2 = "";
	public static String userObj = "";
	public static String userData = "";
	public static int numberLoop;
	public static String done = "";
	
	public static String start()
	{
		//Scanner word = new Scanner(System.in);
		System.out.println("What would you like to do? (Enter \"quit\" to exit):");
		System.out.println("Type the number of action you want to do...");
		System.out.println("1. Add Category");		
		System.out.println("2. Add Object");
		System.out.println("3. Add Data");
		System.out.println("4. Remove Category");
		System.out.println("5. Remove Object");
		System.out.println("6. Search");
		System.out.println("7. View Categories");
		System.out.println("8. View Object Names");
		userChoice = getInput();
		return userChoice;
	}
	
	public static void intro()
	{
		System.out.println("Welcome to the DatabaseSVN Project!");
		System.out.print("How many categories would like to start with? (you can add up to five): ");
		numberLoop = num.nextInt();
		for(int i = 0; i < numberLoop; i++)
		{
			System.out.print("Enter name for new category: ");	
			userCat = getInput();
			mainDatabase.addCategory(userCat);
			System.out.println("Added \"" + userCat + "\" to database");
		}
		System.out.print("Press Enter to continue...");
		done = scanner.nextLine();
	}
	
	public static String getInput()
	{
		Scanner scanner2 = new Scanner(System.in);
		String s = scanner2.nextLine();
		s = s.toLowerCase();
		
		if(s.equals("quit"))
		{
			quit = true;
			System.out.println("Thanky you for using DatabaseSVN!\nCreated by:\nJoe Cavallaro\nMark Johnson\nAndrew Liao");
			process("quit");
		}
		else if(s.equals("cancel"))
		{
			start();
		}
		
		return s;
	}
	
	public static void setNumberLoop()
	{
		Scanner num = new Scanner(System.in);
		try{numberLoop = num.nextInt();}
		catch(InputMismatchException im)
		{
			System.out.println("ERROR: Invalid entry. To return to the main menu type \"cancel\", or press \"Enter\" to re-enter number.");
			userChoice = getInput();
			if(userChoice.equals("cancel"))
			{
				start();
			}
			else if(userChoice.equals("quit"))
			{
				System.out.println("Thank you for using Database SVN!");
				process(userChoice);
			}
			else
			{
				System.out.print("Re-enter number now: ");
				setNumberLoop();
			}
		}
		if(numberLoop <= 0 || numberLoop > 5)
		{
			System.out.println("Invalid number. Must be in the range 1 through 5. Hit \"Enter\" to continue or type cancel to return to the main menu.");
			getInput();
			System.out.print("Re-enter number now: ");
			setNumberLoop();
		}
	}
	
	public static void addCategory()
	{
		System.out.print("Enter name for new category: ");
		userChoice = getInput();
		mainDatabase.addCategory(userChoice);
		System.out.println("Added \"" + userChoice + "\" to database");
		System.out.print("Press Enter to continue...");
		done = scanner.nextLine();
	}
	
	public static void addObject()
	{
		String name;
		System.out.print("Enter name for new Object: ");
		name = getInput();
		mainDatabase.addObject(name);
		mainDatabase.checkCategories();
		System.out.println("Added \"" + name + "\" to database");
		System.out.print("Would you like to add data to your new Object? (Y or N): ");
		userChoice = getInput();
		if(userChoice.equals("y"))
		{
			
			System.out.print("Enter the # of pieces of data are you adding: ");
			
			for(int i = 0; i < numberLoop; i++)
			{
				System.out.println("Enter category to add data to: ");
				mainDatabase.printAllCategories();
				userCat = getInput();					
				System.out.print("Enter data to add: ");
				userData = getInput();
				mainDatabase.nameSearch2(name).addStuff(userCat, userData);
				System.out.println("Added \"" + userData + "\" to category \"" + userCat + "\" for object \"" + name + "\".");
			}
		}
		
		else if(userChoice.equals("n"))
		{
			System.out.println("No data will be added");
		}
		
		else
		{
			System.out.println("Invalid entry, returning to main menu");
		}
		
		System.out.println("Press Enter to continue...");
		done = scanner.nextLine();		
	}
	
	public static void addData()
	{
		System.out.print("Enter name of object to add data to: ");
		userObj = getInput();		
		userObj.toLowerCase();
		System.out.print("Enter the # of pieces of data are you adding (up to 5): ");
		setNumberLoop();
		for(int i = 0; i < numberLoop; i++)
		{
			System.out.println("Enter category to add data to: ");
			mainDatabase.printAllCategories();
			userCat = getInput();
			System.out.print("Enter data to add: ");
			userData = getInput();
			(mainDatabase.nameSearch2(userObj)).addStuff(userCat, userData);
			System.out.println("Added \"" + userData + "\" to category \"" + userCat + "\" for object \"" + userObj);
		}
		System.out.print("Press Enter to continue...");
		done = scanner.nextLine();
	}
	
	public static void removeCategory()
	{
		if(mainDatabase.categories.size() > 1)
		{
			System.out.println("Enter name of category to be removed: ");
			mainDatabase.printAllCategories();
			userChoice = getInput();
			int category = mainDatabase.getCatPlace(userChoice);
			mainDatabase.removeCategory(category);
			System.out.println("Removed category \"" + userChoice + "\" from database");
			System.out.print("Press Enter to continue...");
			done = scanner.nextLine();
		}
		else
		{
			System.out.println("There is currently only 1 category.");
			System.out.println("You must have at least 1 category.");
			System.out.println("To remove this category, please add another category first.");
			System.out.println("Returning to main menu.");
			
		}
	}
	
	public static void removeObject()
	{
		System.out.print("Enter name of object to be deleted: ");
		mainDatabase.printObjectNames();
		userChoice = getInput();
		if(mainDatabase.objectExist(userChoice))
		{
			mainDatabase.removeObject(userChoice);
			System.out.println("Removed \"" + userChoice + "\" from database.");
			System.out.print("Press Enter to continue...");
		}
		else
		{
			System.out.println("Object \"" + userChoice + "\" not found in database.");
			System.out.println("Press Enter to continue...");
			done = scanner.nextLine();
		}
		
		done = scanner.nextLine();
	}
	
	public static void search()
	{
		System.out.println("What type of search would you like to perform: ");
		System.out.println("1. Search for Object with data in specific category");
		System.out.println("2. Search for Object by name");
		System.out.print("Enter number of choice: ");
	
		userChoice = getInput();
		
		if(userChoice.equals("1"))
		{
			System.out.println("Enter category name: ");
			mainDatabase.printAllCategories();
			userCat = getInput();
			if(mainDatabase.categoryExist(userCat))
			{
				System.out.print("Enter data to be searched for: ");
				userData = getInput();
				mainDatabase.categorySearch(userCat, userData);
				System.out.print("Press enter to continue...");
				done = scanner.nextLine();
			}
			else
			{
				System.out.println("Category \"" + userCat + "\" does not exist");
				System.out.print("Press enter to continue...");
				done = scanner.nextLine();
			}
			
		}
		else if(userChoice.equals("2"))
		{
			
			System.out.println("Enter name of Object to be printed: ");
			mainDatabase.printAllObjectNames();
			userData = getInput();
			if(mainDatabase.objectExist(userData))
			{
				mainDatabase.nameSearch(userData);
			}
			else
			{
				System.out.println("Object \"" + userData + "\" not found in database.");
			}
			
			System.out.print("Press enter to continue...");
			done = scanner.nextLine();
		}
	}
	
	public static void viewAllCategories()
	{
		System.out.println("The following categories were found:");
		mainDatabase.printAllCategories();
		System.out.print("Press enter to continue...");
		done = scanner.nextLine();
	}
		
	public static void viewAllObjects()
	{
		System.out.println("The following objects were found:");
		mainDatabase.printObjectNames();
		System.out.print("Press enter to continue...");
		done = scanner.nextLine();
	}
	
	public static void process(String s)
	{
		if(s.equals("1"))
			addCategory();
		else if(s.equals("2"))
			addObject();
		else if(s.equals("3"))
			addData();
		else if(s.equals("4"))
			removeCategory();
		else if(s.equals("5"))
			removeObject();
		else if(s.equals("6"))
			search();
		else if(s.equals("7"))
			viewAllCategories();
		else if(s.equals("8"))
			viewAllObjects();
		else if(s.equals("quit"))
		{
			return;
		}
		else
		{
			System.out.print("Invalid entry, please re-enter your choice: ");
			String reenter = getInput();
			process(reenter);
		}
	}
	
	
	
	public static void main(String args[])
	{
		intro();
		while(quit == false)
		{
			process(start());
		}

	}
	
}
